Back to Main Menu

Get Asset, Components, and Dimensions (Assetic Python SDK)

The article Asset Tools Assetic Python SDK illustrates how to use a set of Asset Tools in the Python SDK to get an asset and it's components and dimensions using a single method.  To understand how this tool is able to consolidate these records, the following code sample works through a series of steps to build up information about the asset.

 

  1. """
  2. Get an asset including component and dimensions (Assetic.AssetsGetComplete.py)
  3. """
  4. import assetic
  5. # Assetic SDK instance
  6. asseticsdk=assetic.AsseticSDK("c:/users/you/assetic.ini", None,"Debug")
  7. #Asset API
  8. assetapi = assetic.AssetApi()
  9. #Component API
  10. componentapi = assetic.ComponentApi()
  11. #Tool API
  12. assettools = assetic.AssetTools()
  13. def main(assetid,attributes = None):
  14. """
  15. Given an asset ID get the asset, components, and component dimensions
  16. :param assetid: Assetic asset GUID or user friendly asset ID
  17. """
  18. ##get the asset
  19. #asset = get_asset(assetid)
  20. if asset == None:
  21. return
  22. #get some of the returned info
  23. assetid = asset.get("AssetId")
  24. assetguid = asset.get("Id")
  25. attributes = asset.get("Attributes")
  26. zone = attributes.get("Zone")
  27. locality = attributes.get("Locality")
  28. msg = "Asset - guid: {0}, Asset: {1}, Zone: {2}, Locality: {3}"\
  29. .format(assetguid,assetid,zone,locality)
  30. asseticsdk.logger.info(msg)
  31. ##get the components of the asset
  32. searchfilter = "AssetId='{0}'".format(assetid)
  33. componentlist = get_component_list_by_filter(searchfilter)
  34. if componentlist == None:
  35. return
  36. for row in componentlist:
  37. msg = "Asset has component {0}".format(row["Name"])
  38. asseticsdk.logger.info(msg)
  39. ##now get a single component by ID
  40. componentid = componentlist[0]["Id"]
  41. component = get_component(componentid)
  42. #get some of the returned info
  43. if component != None:
  44. clabel = component["Label"]
  45. ctype = component["ComponentType"]
  46. msg = "Component - Label: {0}, Type: {1}".format(clabel,ctype)
  47. asseticsdk.logger.info(msg)
  48. ##get dimensions
  49. dimensions = get_dimensions(componentid)
  50. #get some of the returned info
  51. if dimensions != None:
  52. total = dimensions["TotalResults"]
  53. asseticsdk.logger.info("Number of dimension records: {0}".format(total))
  54. for dim in dimensions["ResourceList"]:
  55. dimguid = dim["Id"]
  56. dmeasure = dim["NetworkMeasure"]
  57. dunit = dim["Unit"]
  58. msg = "Dimension - guid: {0}, Measure: {1}, Unit: {2}".format(
  59. dimguid,dmeasure,dunit)
  60. asseticsdk.logger.info(msg)
  61. def get_asset(assetid):
  62. """
  63. Get an asset for the given asset ID
  64. :param assetid: Assetic asset GUID or user friendly asset ID
  65. :return: asset response object or None
  66. """
  67. asseticsdk.logger.info("Get the asset {0}".format(assetid))
  68. #api will always return a predefined set of core fields
  69. #define additional asset attribute fields, must define at least one
  70. #attribute field names are the internal field names, get using metadata api
  71. attributes = list()
  72. attributes.append("Zone")
  73. attributes.append("Locality")
  74. try:
  75. asset = assetapi.asset_get(assetid,attributes)
  76. except assetic.rest.ApiException as e:
  77. if e.status == 404:
  78. asseticsdk.logger.error("Asset ID {0} not found".format(assetid))
  79. else:
  80. asseticsdk.logger.error("Status {0}, Reason: {1} {2}".format(
  81. e.status,e.reason,e.body))
  82. return None
  83. return asset
  84. def get_component(componentid):
  85. """
  86. Get an component for the given component ID
  87. :param componentid: Assetic component GUID or user friendly component ID
  88. :return: component response object or None
  89. """
  90. asseticsdk.logger.info("Get the component {0}".format(componentid))
  91. try:
  92. component = componentapi.component_get(componentid)
  93. except assetic.rest.ApiException as e:
  94. if e.status == 404:
  95. msg = "Component for Component GUID {0} not found".format(
  96. componentid)
  97. asseticsdk.logger.error(msg)
  98. else:
  99. msg = "Status {0}, Reason: {1} {2}".format(e.status,e.reason,e.body)
  100. asseticsdk.logger.error(msg)
  101. return None
  102. return component
  103. def get_component_list_by_filter(searchfilter):
  104. """
  105. Get an component for the given component ID
  106. :param searchfilter: Search filter, same format as aset search filter
  107. :return: component array or None
  108. """
  109. kw = {'request_params_page':1,
  110. 'request_params_page_size':500,
  111. 'request_params_sorts':'Name-desc',
  112. 'request_params_filters':searchfilter}
  113. asseticsdk.logger.info("Get the components for search filter {0}".format(
  114. searchfilter))
  115. try:
  116. component = componentapi.component_get_0(**kw)
  117. except assetic.rest.ApiException as e:
  118. msg = "Status {0}, Reason: {1} {2}".format(e.status,e.reason,e.body)
  119. asseticsdk.logger.error(msg)
  120. return None
  121. if component["TotalResults"] > 0:
  122. return component["ResourceList"]
  123. else:
  124. return None
  125. def get_dimensions(componentid):
  126. """
  127. Get an dimension array for the given component ID
  128. :param componentid: Assetic component GUID or user friendly component ID
  129. :return: dimension response object or None
  130. """
  131. asseticsdk.logger.info("Get the dimensions for component {0}".format(
  132. componentid))
  133. try:
  134. dimensions = componentapi.component_get_dimension(componentid)
  135. except assetic.rest.ApiException as e:
  136. if e.status == 404:
  137. msg = "Dimensions for Component {0} not found".format(assetguid)
  138. asseticsdk.logger.error(msg)
  139. else:
  140. msg = "Status {0}, Reason: {1} {2}".format(e.status,e.reason,e.body)
  141. asseticsdk.logger.error(msg)
  142. return None
  143. return dimensions
  144. ##This will run the method main() to kickstart it all
  145. if __name__ == "__main__":
  146. assetid = "411142055"
  147. main(assetid,["Zone","Comment"])